package com.clf.data.redis.core.json;

import com.alibaba.fastjson.util.IOUtils;

/**
 * 序列化解析器中字符串的缓存池，加快byte转string的解析速度
* @author <a href="mailto:zhudelie@zuozh.com">zhudelie</a>
* @version 
* @since JDK 1.6
* create on 2017年3月31日
* Copyright 2017 ZZJR All Rights Reserved.
*
*/
public class FastJsonSerializerStringPool {
	private final static ThreadLocal<char[]> charsLocal = new ThreadLocal<char[]>();
	private static char[] allocateChars(int length) {
        char[] chars = charsLocal.get();
        if (chars == null) {
            if (length <= 1024 * 64) {
                chars = new char[1024 * 64];
                charsLocal.set(chars);
            } else {
                chars = new char[length];
            }
        } else if (chars.length < length) {
            chars = new char[length];
        }
        return chars;
	}
	public static String bytes2String(byte[] bytes){
		char[] chars = allocateChars(bytes.length);
		int len = IOUtils.decodeUTF8(bytes,0, bytes.length, chars);
		return new String(chars, 0, len);
	}
}
